package com.assembly.oauth2.config;

import com.alibaba.fastjson.JSON;
import com.assembly.oauth2.service.JwtUser;
import com.assembly.util.properties.BlogProperties;
import com.assembly.util.properties.ImWechatProperties;
import com.assembly.util.tools.TLSSigAPIv2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 扩展响应的认证信息
 */
@Component
public class JwtTokenEnhancer implements TokenEnhancer {

    @Autowired
    private BlogProperties blogProperties;

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken,
                                     OAuth2Authentication oAuth2Authentication) {
        JwtUser user = (JwtUser) oAuth2Authentication.getPrincipal();
        Map<String, Object> map = new HashMap<>();
        map.put("userInfo", JSON.toJSON(user));
        ImWechatProperties wechatIm = blogProperties.getWechatIm();
        TLSSigAPIv2 tlsSigAPIv2 = new TLSSigAPIv2(wechatIm.getSdkAppId(), wechatIm.getKey());
        String s = tlsSigAPIv2.genUserSig(user.getUid(), wechatIm.getExpire());
        map.put("ImToke", s);
        // 设置附加信息
        ( (DefaultOAuth2AccessToken)oAuth2AccessToken ).setAdditionalInformation(map);

        return oAuth2AccessToken;
    }

}
